package com.wowangz.cms.systemmanage.email.set.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.api.UserContext;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.email.set.entity.SystemEmail;

public class SystemEmailService extends BaseService{

	public void emailSetInsert(SystemEmail systemEmail) throws ServiceException {
		try {
			this.getSqlSession().insert("SystemEmailService.emailSetInsert", systemEmail);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	
	/**
	 * @描述： 修改
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:53:58
	 * @param emailSet
	 * @throws ServiceException
	 */
	public void emailSetUpdate(SystemEmail systemEmail) throws ServiceException {
		try {
			this.getSqlSession().update("SystemEmailService.emailSetUpdate", systemEmail);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	/**
	 * @描述：逻辑删除
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:52 55
	 * @param smsSet
	 * @throws ServiceException
	 */
	public void emailSetDelete(SystemEmail systemEmail) throws ServiceException {
		try {
			this.getSqlSession().update("SystemEmailService.emailSetUpdate", systemEmail);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	/**
	 * @描述：查询一条记录
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:55:02
	 * @param paramMap
	 * @return SmsSet
	 * @throws ServiceException
	 */
	public SystemEmail queryEmailSetById(Map<String, Object> paramMap) throws ServiceException {
		try {
			SystemEmail email = this.getSqlSession().selectOne("SystemEmailService.queryEmailSetById", paramMap);
			return email;
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	/**
	 * 查询邮件模板
	 * @param paramMap
	 * @return
	 * @throws ServiceException
	 */
	public SystemEmail queryEmailSetByMap(Map<String, Object> paramMap) throws ServiceException {
		try {
			SystemEmail email = this.getSqlSession().selectOne("SystemEmailService.queryEmailSetByMap", paramMap);
			return email;
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	/**
	 * @描述：查询结果集的数量
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:55:51
	 * @param paramMap
	 * @return long
	 * @throws ServiceException
	 */
	public long queryEmailSetCount(Map<String, Object> paramMap) throws ServiceException {
		try {
			return this.getSqlSession().selectOne("SystemEmailService.queryEmailSetCount", paramMap);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	/**
	 * @描述：查询结果集
	 * @author HUANGYUAN
	 * @TIME 2015年10月9日 下午2:57:08
	 * @param paramMap
	 * @return List<SmsSet>
	 * @throws ServiceException
	 */
	public List<SystemEmail> queryEmailSetPage(Map<String, Object> paramMap) throws ServiceException {
		try {
			return this.getSqlSession().selectList("SystemEmailService.queryEmailSetPage", paramMap);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
	
	public Map<String, String> getMap(SystemEmail email,UserContext userContext) {
		Set<String> parameter = SysConstant.TemplateEmailType.getEmailSetParameter(email.getTemplatetype().toString());
		Map<String, String> variable = new HashMap<String, String>();
		for (Iterator<String> iterator = parameter.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			if (StringUtils.equals(key, "password")) {
				variable.put(key, userContext.getLoginUser().getPassword());
			} else if (StringUtils.equals(key, "amount")) {
				variable.put(key, "10");
			} else if (StringUtils.equals(key, "username")) {
				variable.put(key, userContext.getLoginUser().getUsername());
			} else if (StringUtils.equals(key, "curenttime")) {
				variable.put(key, new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
			}
		}
		return variable;
	}
}
